//
//  CommitViewController.swift
//  GitTFGUI
//
//  Created by Samuel Richard Magny on 2015-09-03.
//  Copyright (c) 2015 Samuel Richard Magny. All rights reserved.
//

import Cocoa
import TFSClient
import SwiftTools

class CommitViewController: NSViewController, NSTextFieldDelegate, TFSClientViewControllerProtocol {

    static let storyboardId = "CommitViewController"
    
    @IBOutlet var commitCommentTextField: NSTextField!
    @IBOutlet var commitButton: NSButton!
    @IBOutlet var commitAndCheckinButton: NSButton!
    @IBOutlet var cancelButton: NSButton!
    
    var tfsClient:ExtendedTFSClient?
  
    
    
    override func viewWillDisappear() {
        super.viewWillDisappear()
        
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()

        commitCommentTextField.delegate = self;
        
        if let tfsClient = self.tfsClient {
            
        }
        
        isReadyToCommit()
        
    }

    
    //Commit actions
    //----------------------------------------------------------
    @IBAction func commitButtonPressed(sender: AnyObject) {
        tfsClient?.commit(commitCommentTextField.stringValue, completionHandler: { (withSuccess) -> () in
            self.cancelButtonPressed(self)
        })
    }
    
    @IBAction func cancelButtonPressed(sender: AnyObject) {
        self.dismissController(sender);
    }
    
    @IBAction func commitAndCheckinButtonPressed(sender: AnyObject) {
        tfsClient?.commit(commitCommentTextField.stringValue, completionHandler: { (withSuccess) -> () in
            
        })
    }

    //NSTextFieldDelegate
    //----------------------------------------------------------
    override func controlTextDidChange(aNotification: NSNotification) {
        
        if(aNotification.object! as! NSObject == self.commitCommentTextField) {
            isReadyToCommit()
        }
    }
    
    
    //Privates
    //----------------------------------------------------------
    func isReadyToCommit()->Bool{
        
        let enableCommit = !commitCommentTextField.stringValue.isEmpty
        commitButton.enabled = enableCommit
        commitAndCheckinButton.enabled = enableCommit
        
        return enableCommit
    }
    
}
